<template>
  <div class="register">
    <img  :src="imgSrc" width="100%" height="100%" alt=""/>
    <div class="registerPart">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <h1>存济口腔管理系统</h1>

        <el-form-item prop="userName" class="inputElement">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input v-model="loginForm.userName" name="userName" type="text" auto-complete="on" placeholder="userName" />
        </el-form-item>

        <el-form-item prop="password" class="inputElement2">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :type="pwdType"
            v-model="loginForm.password"
            name="password"
            auto-complete="on"
            placeholder="password"
            />

          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>

        <el-form-item class="inputElement3">
          <el-button :loading="loginLoading" type="primary" @click.native.prevent="handleLogin" class="left_btn">
            登录
          </el-button>
          <el-button :loading="registrationLoading" type="primary" @click.native.prevent="handleRegistration">
            注册
          </el-button>
        </el-form-item>
    </el-form>
    </div>
    </div>
</template>

<script>
  import { validUsername } from '@/utils/validate'

  export default
  {
    name: 'Login',
    data() {
      const validateUsername = (rule, value, callback) =>
      {
        if (value.trim() === "") {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      };
      const validatePass = (rule, value, callback) =>
      {
        if (value.length < 5)
        {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      };
      return{
        imgSrc: require('@/assets/login/loginImage.jpg'),
        loginForm: {
          userName: 'admin',
          password: '123456'
        },
        loginRules: {
          userName: [{ required: true, trigger: 'blur', validator: validateUsername }],
          password: [{ required: true, trigger: 'change', validator: validatePass }]
        },
        loginLoading: false,
        registrationLoading: false,
        pwdType: 'password',
        redirect: undefined
      }
    },
    watch:
    {
      $route:
      {
        handler: function(route)
        {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password')
        {
          this.pwdType = '';
        } else
        {
          this.pwdType = 'password'
        }
      },
      // 登录
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loginLoading = true;
            this.$store.dispatch('Login', this.loginForm).then(() =>
            {
              this.loginLoading = false;
              this.$router.push({ path: this.redirect || '/' })
            }).catch(() => {
              this.loginLoading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      // 注册
      handleRegistration()
      {
        this.$refs.loginForm.validate(valid =>
        {
          if (valid)
          {
            this.registrationLoading = true;
            this.$store.dispatch('Registration', this.loginForm).then(() =>
            {
              this.registrationLoading = false;
              this.$router.push({ path: this.redirect || '/' });
            }).catch(() => {
              this.registrationLoading = false;
            })
          } else {
            console.log('error submit!!');
            return false
          }
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .left_btn
  {
    margin-left: 8px;
    margin-right: 50%;
  }

  .register {
    width: 100%;
    height: 100%;
  }

  /*最大的那个框*/
  .registerPart
  {
    position: absolute;
    /*定位方式绝对定位absolute*/
    top: 50%;
    left: 50%;
    /*顶和高同时设置50%实现的是同时水平垂直居中效果*/
    transform: translate(-50%, -50%);
    /*实现块元素百分比下居中*/
    width: 450px;

    padding: 20px;
    /*背景颜色为黑色，透明度为0.8*/
    box-sizing: border-box;
    /*box-sizing设置盒子模型的解析模式为怪异盒模型，
    将border和padding划归到width范围内*/
    box-shadow: 0px 15px 25px rgba(0, 0, 0, .5);
    /*边框阴影  水平阴影0 垂直阴影15px 模糊25px 颜色黑色透明度0.5*/
    border-radius: 15px;
    /*边框圆角，四个角均为15px*/
  }

  .registerPart h1
  {
    margin: 0 0 30px;
    padding: 0;
    color: #0C9CCC;
    text-align: center;
    /*文字居中*/
  }

  /*账号框*/
  .registerPart .inputElement
  {
    border: none;
    height: 100px;
  }

  /*密码框*/
  .registerPart .inputElement2
  {
    border: none;
    height: 100px;
  }

  /*登录框*/
  .registerPart .inputElement3
  {
    border: none;
  }

  /*账号输入框*/
  ::v-deep .registerPart .inputElement input
  {
    font-size: 18px;
    margin-left: 20px;
    width: 94%;
    padding: 10px 0;
    color: black;
    letter-spacing: 1px;
    /*字符间的间距1px*/
    border: none;
    border-bottom: 1px solid #fff;
    margin-bottom: 10px;
    outline: none;
    /*outline用于绘制元素周围的线
    outline：none在这里用途是将输入框的边框的线条使其消失*/
    background: transparent;
    /*背景颜色为透明*/
  }

  /*密码输入框*/
  ::v-deep .registerPart .inputElement2 input
  {
    font-size: 18px;
    margin-left: 20px;
    width: 94%;
    padding: 10px 0;
    color: black;
    letter-spacing: 1px;
    /*字符间的间距1px*/
    border: none;
    border-bottom: 1px solid #fff;
    margin-bottom: 10px;
    outline: none;
    /*outline用于绘制元素周围的线
    outline：none在这里用途是将输入框的边框的线条使其消失*/
    background: transparent;
    /*背景颜色为透明*/
  }

  /* reset element-ui css */
  .el-input
  {
    display: inline-block;
    height: 80px;
    width: 86%;
    input
    {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill
       {
         -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
         -webkit-text-fill-color: #fff !important;
       }
    }
  }

  .el-form-item
  {
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #0099CC;
  }

  .login-container
  {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;
  .login-form
  {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips
  {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
  span
  {
  &:first-of-type
   {
     margin-right: 16px;
   }
  }
  }
  .svg-container
  {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    z-index: 100;
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  }
</style>

